<template>
  <div class="view-container oa-template-list">
    <!--<header class="view-header">
      <div class="header-container">
        <div class="header-title">
          <i class="el-icon-s-grid"></i>
          <span>模板管理</span>
        </div>
        <div class="header-form">
          <div class="header-form-item">
            <el-button icon="el-icon-refresh-right" @click="refresh">刷新</el-button>
          </div>
          <div class="header-form-item">
            <el-button type="success" icon="el-icon-edit" @click="edit">编辑</el-button>
          </div>
          <div class="header-form-item">
            <el-button type="danger" :loading="delLoading" icon="el-icon-delete" @click="del">{{isBatchesDel}}</el-button>
          </div>
          &lt;!&ndash;更多&ndash;&gt;
          &lt;!&ndash;<div class="header-form-item">
            <el-dropdown @command="handleCommand">
              <el-button type="default" :loading="delLoading">
                更多<i class="el-icon-arrow-down el-icon&#45;&#45;right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item icon="el-icon-refresh-right" class="color-refresh-item" command="refresh">刷新</el-dropdown-item>
                <el-dropdown-item icon="el-icon-edit" class="color-edit-item" command="edit">编辑</el-dropdown-item>
                <el-dropdown-item icon="el-icon-delete" class="color-del-item" command="del">{{isBatchesDel}}</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>&ndash;&gt;
          &lt;!&ndash;添加&ndash;&gt;
          <div class="header-form-item">
            <el-button type="primary" icon="el-icon-plus" @click="save">添加</el-button>
          </div>
&lt;!&ndash;          &lt;!&ndash;搜索&ndash;&gt;
          <div class="header-form-item">
            <el-button type="primary" icon="el-icon-search" @click="fetchData">查询</el-button>
          </div>
          &lt;!&ndash;按标题查询&ndash;&gt;
          <div class="header-form-item title-search">
            <el-input v-model="oaTemplate1.tname" placeholder="模板名称"></el-input>
          </div>
          &lt;!&ndash;按类型查询&ndash;&gt;
          <div class="header-form-item title-search">
            <el-select v-model="oaTemplate1.ttype" placeholder="模板类型">
              <el-option v-for="(item,index) in types" :value="item.value" :key="index" :label="item.label"></el-option>
            </el-select>
          </div>&ndash;&gt;
        </div>
      </div>
    </header>-->
    <list-header
      :title="'模板管理'"
      :delText="isBatchesDel"
      :delLoading="delLoading"
      :isNeedSearch="false"
      @del="del"
      @open-edit-dialog="openEditDialog($event)"
      @fetch-data="fetchData"
    >
    </list-header>
    <section class="table-container view-section">
      <el-table
        ref="table"
        :data="tableData"
        stripe
        border
        @selection-change="handleSelectionChange"
        @row-click="handleRowClick"
      >
        <el-table-column type="selection" align="center"/>
        <el-table-column prop="tname" label="模板名称" align="left" />
        <el-table-column prop="userNameStr" label="流程用户" align="center" width="250" />
        <el-table-column prop="ttype" label="模板类型" align="center" width="200" sortable/>
        <el-table-column prop="sortIndex" label="序号" align="center" width="100" />
      </el-table>
    </section>

    <footer>
      <el-pagination
        :page-sizes="[10, 20, 50, 100]"
        style="text-align: right"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        :page-size="page.size"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </footer>

    <el-dialog
      :title="dialog.title"
      :visible.sync="dialog.show"
      :before-close="beforeClose"
      :close-on-click-modal="false"
    >
      <div class="checkerList">
        <el-form :model="oaTemplate" :rules="rules" ref="form" label-width="80px">
          <el-form-item label="模板名称" prop="tname">
            <el-input v-model.trim="oaTemplate.tname"></el-input>
          </el-form-item>
          <el-form-item label="模板类型" prop="ttype">
            <el-select v-model="oaTemplate.ttype">
              <el-option v-for="(item,index) in types" :value="item.label" :key="index" :label="item.label"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="添加用户" prop="userIdList">
            <el-select v-model="oaTemplate.userIdList" multiple>
              <el-option v-for="(item,index) in users" :value="item.value" :key="index"
                         :label="item.label"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="序号" prop="sortIndex">
            <el-input-number v-model="oaTemplate.sortIndex" @change="handSortIndexChange" :min="1"
                             :max="99"></el-input-number>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer">
        <el-button
          type="success"
          @click="submitForm"
          :loading="submitLoading">确 定
        </el-button>
        <el-button type="default" @click="closeDialog">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import oaTemplateApi from '@/api/system/oaTemplate'
  import userAPI from '@/api/system/user'
  import listMixin from '@/mixin/listMixin'
  import ListHeader from '@/common/ListHeader'
  export default {
    name: 'ReceiptList',
    components:{ListHeader},
    mixins: [listMixin],
    data() {
      return {
        showDialog: false,
        submitLoading: false,
        oaTemplate: {
          sortIndex: 0,
          tid: "",
          tname: "",
          ttype: "",
          userIdList: []
        },
/*        searchForm: {
          tname: "",
          ttype: "",
        },*/
        rules: {
          tname: [
            {required: true, message: '请输入模板名称', trigger: 'blur'}
          ],
          ttype: [
            {required: true, message: '请选择模板类型', trigger: 'change'}
          ],
          userIdList: [
            {required: true, message: '请选择用户', trigger: 'change'}
          ],
        },
        types: [],
        users: [],
        mixinParams: {
          addUrl: '',
          editUrl: '',
          propertyName: 'tid',
          API: oaTemplateApi
        },
        dialog:{
          title:'',
          show:false
        }
      }
    },
    created() {
      this.getTemplateType()
      this.getUserList()
    },
    computed: {
    },
    methods: {
      typeFmt(row) {
        console.log(row.ttype)
        for (const val of this.types) {
          if (val.value === parseInt(row.ttype)) {
            return val.label
          }
        }
      },
      handSortIndexChange(val) {

      },
      getUserList() {
        userAPI.getSelectList('').then(res => {
          this.users = res.result
          console.log(this.users)
        })
      },
      getTemplateType() {
        oaTemplateApi.getTemplateType().then(res => {
          console.log('getTemplateType',res.result)
          this.types = res.result
        }).catch(err => {
          console.log(err)
        })
      },
      closeDialog() {
        this.resetForm()
        this.dialog.show = false
      },
      beforeClose(done) {
        this.resetForm()
        done(true)
      },
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            this.saveOrUpdate()
          }
        })
      },
      saveOrUpdate() {
        this.submitLoading = true
        const promise = this.oaTemplate.tid ? oaTemplateApi.update(this.oaTemplate) : oaTemplateApi.save(this.oaTemplate)
        promise.then(res => {
          this.submitLoading = false
          this.$message.success('保存成功')
          this.fetchData()
          this.closeDialog()
        }).catch(err => {
          this.submitLoading = false
          console.log(err)
        })
      },
      openEditDialog(isEdit) {
        if(isEdit){
          if(this.multipleSelected.length === 0){
            this.$message.info('请选择要编辑的记录')
            return
          }
          if(this.multipleSelected.length > 1){
            this.$message.info('您一次只能编辑一条记录')
            return
          }
          this.oaTemplate = this.getObjFromTableData('tid')
          console.log(this.oaTemplate)
          this.dialog.show = true
        }else{
          this.dialog.show = true
        }
      },
      refresh() {
        location.reload()
      },
      fetchData() {
        oaTemplateApi.getList(this.page).then(res => {
          console.log(res)
          this.page.total = res.total
          this.tableData = res.records
        }).catch(err => {
          console.log(err)
        })
      },
      resetForm() {
        this.$refs['form'].resetFields()
        this.oaTemplate = {
          sortIndex: 0,
          tid: "",
          tname: "",
          ttype: "",
          userIdList: []
        }
      }
    }
  }
</script>

<style lang="scss">
  .oa-template-list {
    .el-dialog {
      width: 500px;
    }

    .el-select {
      width: 100%;
    }

    .el-dialog__footer {
      padding-bottom: 10px;
      padding-top: 0;
    }
  }

</style>

<style scoped lang="scss">

</style>

